package org.apache.hadoop.contrib.ftp;

import java.io.IOException;

import org.apache.ftpserver.ftplet.FileSystemView;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpReply;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.FtpletContext;
import org.apache.ftpserver.ftplet.FtpletResult;

public class HDFSFTPlet implements Ftplet {

    @Override
    public void init(FtpletContext ftpletContext) throws FtpException {
        // TODO Auto-generated method stub

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public FtpletResult beforeCommand(FtpSession session, FtpRequest request)
            throws FtpException, IOException {
        if (session.getUser() != null) {
            String home = session.getUser().getHomeDirectory();
            if (!home.equals("/") && home.endsWith("/"))
                home = home.substring(0, home.length() - 1);

            String command = request.getCommand();
            FileSystemView view = session.getFileSystemView();
            String arg = request.getArgument();
            if (arg != null) {
                if (arg.startsWith("/")) {
                    return checkPath(command, arg, home);
                } else if (arg.startsWith("..")) {
                    String cwd = view.getWorkingDirectory().getAbsolutePath();
                    String parent = getParent(cwd);
                    arg = parent + arg.substring(2);
                    return checkPath(command, arg, home);
                } else {
                    return FtpletResult.DEFAULT;
                }
            }
//            } if (command.equals("CDUP")){
//                if (view.getWorkingDirectory().getAbsolutePath().equals(home)){
//                    return FtpletResult.SKIP;
//                }else{
//                    return FtpletResult.DEFAULT;
//                }
//            }
            else {
                return FtpletResult.DEFAULT;
            }
        } else {
            return FtpletResult.DEFAULT;
        }
    }

    private FtpletResult checkPath(String command, String arg, String home) {
        if (command.equalsIgnoreCase("DELE")
                || command.equalsIgnoreCase("STOR")
                || command.equalsIgnoreCase("RMD")
                || command.equalsIgnoreCase("MKD")
                || command.equalsIgnoreCase("APPE")
                || command.equalsIgnoreCase("STOU")
                || command.equalsIgnoreCase("RNTO")
//                || command.equalsIgnoreCase("CWD")
                || command.equalsIgnoreCase("MLST")
                || command.equalsIgnoreCase("LIST")
                || command.equalsIgnoreCase("MLSD")) {
            if (arg.startsWith(home)) {
                return FtpletResult.DEFAULT;
            } else {
                return FtpletResult.SKIP;
            }
        } else {
            return FtpletResult.DEFAULT;
        }
    }

    private String getParent(String cwd) {
        if (!cwd.equals("/") && cwd.endsWith("/")) {
            cwd = cwd.substring(0, cwd.length() - 1);
        }
        if (cwd.equals("/")) {
            return "/";
        }
        int index = cwd.lastIndexOf("/");
        if (index > 0) {
            return cwd.substring(0, index);
        } else {
            return "/";
        }
    }

    @Override
    public FtpletResult afterCommand(FtpSession session, FtpRequest request,
            FtpReply reply) throws FtpException, IOException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public FtpletResult onConnect(FtpSession session) throws FtpException,
            IOException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public FtpletResult onDisconnect(FtpSession session) throws FtpException,
            IOException {
        // TODO Auto-generated method stub
        return null;
    }

}
